package net.lzzy.service;

import net.lzzy.entity.Article;
import net.lzzy.entity.Comment;
import net.lzzy.entity.User;
import net.lzzy.repository.ArticleRepository;
import net.lzzy.repository.CommentRepository;
import net.lzzy.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CommentService {

    @Autowired
    private CommentRepository commentRepository;

@Autowired
private ArticleRepository articleRepository;

    @Autowired
    private UserRepository userRepository;

    public List<Comment> findCommentByArticleId(Integer id){
        return commentRepository.findByArticleId(id);
    }

    public void addComment(Integer id, String author, String content){
        Comment comment = new Comment();
        Article article = articleRepository.findById(id).get();
        comment.setArticle(article);
        comment.setAuthor(author);
        comment.setContent(content);
        comment.setDate(new Date());
        commentRepository.save(comment);
    }

    public List<Comment> findAll(){
        return commentRepository.findAll();
    }

    public void deleteById(Integer id){
        commentRepository.deleteById(id);

    }
    public List<User> findUser(){
//        return userRepository.findByUsername(String username);
        List<User> users=userRepository.findAll();
        return users;
    }

    public void deleteUser(Integer id){
        userRepository.deleteById(id);

    }
//    public void updateCommentById(Integer id, Integer a_id, String author, String content){
//        Comment comment = findCommentById(id);
//        comment.setA_id(a_id);
//        comment.setAuthor(author);
//        comment.setContent(content);
//        commentRepository.save(comment);
//
//    }
//
//    //新增文章
//    public Comment addArticle(Integer id, Integer a_id, String author, String content){
//        Comment comment = findCommentById(id);
//        comment.setA_id(a_id);
//        comment.setAuthor(author);
//        comment.setContent(content);
//        comment.setDate(new Date());
//        return commentRepository.save(comment);
//    }
//

}
